<?php
/*
  功能：角色 model
  作者：cxm
  时间：2019-01-11
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class AdminRole extends BaseModel {
//    use SoftDeletes;
//    protected $dates = ['deleted_at']; //开启deleted_at
    protected $table='admin_role';  //绑定表

    /**
     * 权限
     */
    public function permission(){
        return $this->hasMany(AdminRolePermission::class,'role_id','id');
    }

    /**
     * 获取角色列表
     * @param $where 查询条件
     * @param $orderBy 排序
     * @param $page 页码
     * @param $limit 每页显示条数
     * @return mixed
     */
    public static function getData($where,$orderBy,$page,$limit){
        $result = self::where($where);
        if ($orderBy) {
            foreach ($orderBy as $key=>$val) {
                $result->orderBy($val[0], $val[1]);
            }
        }else{
            $result->orderBy('created_at','desc');
        }
        $total = $result->count();
        $data = $result->skip(($page-1)*$limit)->take($limit)->get()->toArray();
        return ['total'=>$total,'data'=>$data];
    }

    /**
     * 查看详情
     * @param $where 条件
     * @return mixed
     */
    public static function detail($where){
        return self::where($where)->first();
    }
}